package com.csgc.camera.widget.dialog

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.StyleRes
import androidx.core.animation.doOnRepeat
import androidx.core.content.ContextCompat

import com.csgc.camera.biz.other.activity.WebViewActivity
import com.csgc.camera.constants.Constants
import com.csgc.camera.util.clickWithTrigger
import com.csgc.camera.widget.dialog.base.AnimAction
import com.csgc.camera.widget.dialog.base.BaseDialog
import com.csgc.camera.widget.dialog.interpolator.BreatheInterpolator
import com.cysd.spsyxj.R

import com.drake.spannable.addSpan
import com.drake.spannable.movement.ClickableMovementMethod
import com.drake.spannable.span.HighlightSpan
import com.hjq.shape.layout.ShapeLinearLayout
import com.hjq.shape.view.ShapeTextView
import com.hjq.toast.ToastUtils


enum class PrivacyStyle{
    DIRECT,VERIFY
}

class PrivacyDialog  constructor(context: Context, @StyleRes themeResId: Int = R.style.BaseDialogTheme): BaseDialog(context,themeResId) {

    private var style:PrivacyStyle = PrivacyStyle.VERIFY
    var listener:PrivacyClickListener?=null

    private val containerLayout: ViewGroup? by lazy { findViewById(R.id.ll_ui_container) }
    private val titleView: TextView? by lazy { findViewById(R.id.tv_ui_title) }

    //outer
    private val cancelView: ShapeTextView? by lazy { findViewById(R.id.tv_ui_cancel) }
    private val confirmView: ShapeTextView? by lazy { findViewById(R.id.tv_ui_confirm) }
    private val handleLayout: LinearLayout? by lazy { findViewById(R.id.layout_verify_handle) }

    private val tvPrivacyProtocol:TextView? by lazy { findViewById(R.id.tv_privacy_protocol) }
    private val outerImageCheck:ImageView? by lazy { findViewById(R.id.iv_check_agree) }
    private var outerCheckStatus = false

    //inner
    private val disagreePrivacy: TextView? by lazy { findViewById(R.id.tv_disagree_privacy) }
    private val agreePrivacy: ShapeLinearLayout? by lazy { findViewById(R.id.layout_agree_privacy) }
    private val innerImageCheck: ImageView? by lazy { findViewById(R.id.iv_inner_check_agree) }

    private val itemColor: Int by lazy {
        ContextCompat.getColor(context, R.color.color_2C81E6)
    }

    init {
        setContentView(R.layout.dialog_privacy)
        setWindowAnimations(AnimAction.ANIM_IOS)
        setGravity(Gravity.CENTER)
        setCancelable(false)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        initEvent()
    }

    private fun initView(){
        if(style == PrivacyStyle.VERIFY){
            tvPrivacyProtocol?.text = "勾选并同意"
                .addSpan("《用户服务协议》", listOf(HighlightSpan(color = itemColor,onClick = {
                    WebViewActivity.launch(context,"用户服务协议", Constants.PrivacyUrl.protocol.url)
                })))
                .addSpan("和")
                .addSpan("《隐私政策》", listOf(HighlightSpan(color = itemColor,onClick = {
                    WebViewActivity.launch(context,"隐私政策",Constants.PrivacyUrl.privacy.url)
                })))
        }else{
            tvPrivacyProtocol?.text = "查看完整版"
                .addSpan("《用户服务协议》", listOf(HighlightSpan(color = itemColor,onClick = {
                    WebViewActivity.launch(context,"用户服务协议",Constants.PrivacyUrl.protocol.url)
                })))
                .addSpan("和")
                .addSpan("《隐私政策》", listOf(HighlightSpan(color = itemColor,onClick = {
                    WebViewActivity.launch(context,"隐私政策",Constants.PrivacyUrl.privacy.url)
                })))
        }

        tvPrivacyProtocol?.movementMethod = ClickableMovementMethod()


    }


    private fun initEvent(){
        cancelView?.clickWithTrigger {
            dismiss()
            listener?.onCancel()
        }
        confirmView?.clickWithTrigger {
            if (!outerCheckStatus){
                ToastUtils.show("请同意隐私协议")
                return@clickWithTrigger
            }
            dismiss()
            listener?.onAgree()
        }
        outerImageCheck?.clickWithTrigger {
            outerCheckStatus = !outerCheckStatus
            if(outerCheckStatus){
                outerImageCheck?.setImageResource(R.drawable.icon_privacy_outer_checked)
            }else{
                outerImageCheck?.setImageResource(R.drawable.icon_privacy_outer_default)
            }
        }

        disagreePrivacy?.clickWithTrigger {
            dismiss()
            listener?.onCancel()
        }
        agreePrivacy?.clickWithTrigger {
            innerImageCheck?.setImageResource(R.drawable.icon_privacy_inner_checked)
            innerImageCheck?.handler?.postDelayed({
                dismiss()
                listener?.onAgree()
            },100)


        }
    }


    fun showDialog(style:PrivacyStyle){
        this.style = style
        initView()
        if (style == PrivacyStyle.VERIFY){
            startAlphaBreathAnimation2(confirmView!!)
            handleLayout?.visibility = View.VISIBLE

        }else{
            startAlphaBreathAnimation(agreePrivacy!!)
            disagreePrivacy?.visibility = View.VISIBLE
            agreePrivacy?.visibility = View.VISIBLE
            outerImageCheck?.visibility = View.GONE
        }

        show()
    }



    private fun startAlphaBreathAnimation(view:View) {
        val alphaAnimatorX = ObjectAnimator.ofFloat(view, "scaleX", 1f, 1.2f)
        val alphaAnimatorY = ObjectAnimator.ofFloat(view, "scaleY", 1f, 1.2f)
        alphaAnimatorX.repeatCount = ValueAnimator.INFINITE
        alphaAnimatorY.repeatCount = ValueAnimator.INFINITE

        AnimatorSet().apply {
            duration = 1000
            interpolator = BreatheInterpolator()
            playTogether(alphaAnimatorX,alphaAnimatorY)
            start()
        }

    }

    private fun startAlphaBreathAnimation2(view:View) {
        val alphaAnimatorX = ObjectAnimator.ofFloat(view, "scaleX", 1f, 1.4f)
        val alphaAnimatorY = ObjectAnimator.ofFloat(view, "scaleY", 1f, 1.2f)
        alphaAnimatorX.repeatCount = ValueAnimator.INFINITE
        alphaAnimatorY.repeatCount = ValueAnimator.INFINITE

        AnimatorSet().apply {
            duration = 1000
            interpolator = BreatheInterpolator()
            playTogether(alphaAnimatorX,alphaAnimatorY)
            start()
        }

    }


    interface PrivacyClickListener{
        fun onCancel()
        fun onAgree()
    }


}